/* vim: set tabstop=4 shiftwidth=4: */
/*****************************************************************************
 * license.c
 * license management
 * 2013-11, Guo init.
 ****************************************************************************/

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <float.h> /* for FLT_DIG, DBL_DIG, LDBL_DIG, etc */
#include <stdint.h> /* for uint32_t, etc */
#include "od_engine.h"

//#include "suma_trace_log.h" 
#include "hard_inf.h"
//#include "decrypt.h"

const char licence_file_name[64] = "/mnt/flash/sav/licence.txt";

#ifdef LICENCE_ENABLE

static unsigned char secretKey[256] = {
0xb7, 0x5f, 0x71, 0x3a, 0xa5, 0x57, 0xb0, 0x0d, 0x81, 0x6b, 0xb0, 0x64, 0xb6, 0x27, 0x4f, 0x7f, 
0xa9, 0x95, 0xd7, 0xe9, 0x00, 0xb5, 0x92, 0x34, 0x04, 0x8b, 0x1c, 0xc2, 0x38, 0x7d, 0x9c, 0xd0, 
0x8d, 0x6d, 0x43, 0xb4, 0xc2, 0x25, 0xd2, 0x49, 0xca, 0x10, 0x53, 0x6a, 0xbf, 0x65, 0x2c, 0x0e, 
0x90, 0xd1, 0xe2, 0xe9, 0xc9, 0x07, 0xdb, 0xb4, 0xb8, 0x94, 0x36, 0x79, 0x34, 0xbf, 0x5a, 0xbd, 
0xc1, 0xd2, 0x1f, 0xef, 0x60, 0x6b, 0x7c, 0x6b, 0x2e, 0x47, 0x37, 0x0a, 0xeb, 0x63, 0x58, 0x57, 
0xac, 0x27, 0x9a, 0xa3, 0x5c, 0x10, 0x59, 0xb2, 0xb5, 0xd2, 0x2c, 0x89, 0x5c, 0xb1, 0x38, 0xf0, 
0x19, 0xf9, 0xcb, 0x78, 0x50, 0x41, 0xaa, 0x26, 0x0b, 0x11, 0xe2, 0x6f, 0xc9, 0xd5, 0x16, 0xfa, 
0x3f, 0x98, 0x90, 0xcb, 0x1b, 0x53, 0xb7, 0x86, 0xf4, 0x46, 0x24, 0xa6, 0x25, 0x85, 0xc8, 0x91, 
0xec, 0x4a, 0xb1, 0xb5, 0x16, 0x65, 0x27, 0x1f, 0xc1, 0x16, 0x58, 0x06, 0x1e, 0xb2, 0xd7, 0xc0, 
0x0b, 0x34, 0xfb, 0x5e, 0xd0, 0x69, 0xdc, 0xf5, 0x6d, 0x6d, 0x1d, 0x78, 0xce, 0x6d, 0x6e, 0xaf, 
0x3c, 0xec, 0xd0, 0xc5, 0x52, 0x07, 0xcc, 0xd8, 0x1f, 0xf6, 0x94, 0xee, 0x4a, 0xe9, 0x7c, 0x26, 
0x4f, 0x70, 0xfc, 0xaf, 0x33, 0xb9, 0xc0, 0x16, 0x16, 0x1e, 0x54, 0x1c, 0xf8, 0x76, 0x2a, 0x7d, 
0xc2, 0x8a, 0xcb, 0xc1, 0x5b, 0x07, 0xaa, 0xac, 0x9d, 0x0a, 0x55, 0xb2, 0xef, 0x7c, 0xf9, 0x4f, 
0x81, 0xe6, 0xac, 0xdd, 0x6a, 0x2e, 0xbd, 0x66, 0xbb, 0x1e, 0x07, 0x0e, 0x31, 0x11, 0x18, 0x77, 
0xa1, 0x9e, 0x43, 0xab, 0xe2, 0x0d, 0x9d, 0xcf, 0x68, 0xdc, 0x21, 0x4b, 0xcb, 0x71, 0xb0, 0x1d, 
0xe7, 0x8f, 0xbd, 0xb3, 0xdc, 0xa7, 0x63, 0xf7, 0x9e, 0x5b, 0x77, 0x0d, 0xda, 0x04, 0x84, 0xf8, 
 
};




const char tmp_mount_dir[32]="/media/";
const char tmp_mount_dev[32]="/dev/mtdblock2";

static void licence_mount(const char dir[], const char dev[])
{
    char temp[64];
    FILE* cmdFile;
    
    sprintf(temp , " mount -t jffs2 %s %s" ,dev,  dir);
    cmdFile = popen(temp, "r");
    pclose(cmdFile);
}

static void licence_umount(const char dir[])
{
    char temp[64];
    FILE* cmdFile;
    
    sprintf(temp , " umount %s" ,dir);
    cmdFile = popen(temp, "r");
    pclose(cmdFile);
}

#endif

/*1:means yes;0 means not*/
static int is_character(char c)
{
    if((c >= '!') &&(c <= '~'))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

static void str_check(char *str, int len)
{
    int i = 0;
    
    for(i = 0; i < len; i++)
    {
        /*不是合法字符则终止*/
        if(1 != is_character(str[i]))
        {
            str[i] = '\0';
        }
    }
}

int read_hard_inf(const char * file_name, hard_information_t *hard_inf)
{
    FILE *fp = NULL;
    char buf[HARD_INFO_STR_LEN];
    int len = 0;
	int ret = 0;
   // int i = 0;

    
    /*if file open success,read file data to set hardware information*/
    if ((fp = fopen(file_name, "r")) != NULL)
    {
        /*first PCB version*/
        if(fgets(buf, HARD_INFO_STR_LEN, fp) != NULL)
        {
            /*cut the last "/n"*/
            len = strlen(buf);
            str_check(buf, len);
            strncpy(hard_inf->hard_ver, buf, len);
        }
        else
        {
            ret = 1;
        }

        /*second serial number*/
        if(fgets(buf, HARD_INFO_STR_LEN, fp) != NULL)
        {
            len = strlen(buf);
            str_check(buf, len);
            strncpy(hard_inf->sn, buf, len);
        }
        else
        {
            ret = 1;
        }

        /*third mac number*/
        if(fgets(buf, HARD_INFO_STR_LEN, fp) != NULL)
        {
            len = strlen(buf);
            str_check(buf, len);
            strncpy(hard_inf->mac1, buf, len);

        }
        else
        {
            ret = 1;
        }
        
#ifndef PROJ9550A_V1
        /*four mac number*/
        if(fgets(buf, HARD_INFO_STR_LEN, fp) != NULL)
        {
            len = strlen(buf);
            str_check(buf, len);
            strncpy(hard_inf->mac2, buf, len);
        }
        else
        {
            ret = 1;
        }
#endif
        fclose(fp);
    }
    else{
        /*else if file open failed, use the default information*/
        RPT(RPT_WRN,"%s open failed,use default information",file_name);
        ret = 1;
    }
    
    return ret;
}

int read_licence(char licence[], int maxlen)
{
    FILE *fp = NULL;
    char buf[HARD_INFO_STR_LEN];
//    int len = 0;
	int ret = -1;
    /*should read PCB_version, sn,mac*/
    
    /*if file open success,read file data to set hardware information*/
    if ((fp = fopen(licence_file_name, "r")) != NULL)
    {
        /*first PCB version*/
        if(fgets(buf, HARD_INFO_STR_LEN, fp) != NULL)
        {
            /*cut the last "/n"*/
       //     len = strlen(buf);
            strncpy(licence, buf, maxlen);
            ret = 0;
        }

        fclose(fp);

    }
    else{
        /*else if file open failed, use the default information*/
        printf("%s open failed,use default information\n",licence_file_name);
    }
    
    return ret;
}

